package br.com.rampage.controller.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.rampage.model.bo.PacienteBO;
import br.com.rampage.utils.ConnectionUsersUtil;

/**
 * Servlet implementation class AlterarDadosServlet
 */
@WebServlet("/AlterarDadosServlet")
public class AlterarDadosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AlterarDadosServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String cpf = request.getParameter("cpf");
		System.out.println(cpf);
		String email = request.getParameter("email");
		String senha = request.getParameter("senha");
		String confirmar_senha = request.getParameter("confirmar_senha");

		boolean success = new PacienteBO().alterar(cpf, email, senha,
				confirmar_senha);

		if (success) {
			response.setContentType("text/html;charset-UTF-8");
			PrintWriter writer = response.getWriter();
			new Message().print(writer, TypeReturnServlet.success,
					"Dados alterados com sucesso!");
			
			ConnectionUsersUtil.removeUserSession(request.getSession().getId());
			HttpSession session = request.getSession(true);
			ConnectionUsersUtil.setUserOfSessionId(session.getId(), new PacienteBO().carregar(cpf));
		} else {
			response.setContentType("text/html;charset-UTF-8");
			PrintWriter writer = response.getWriter();
			new Message().print(writer, TypeReturnServlet.danger,
					"Dado(s) incorreto(s)!");
		}
		

		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/alterar_dados.jsp");
		dispatcher.include(request, response);

	}

}
